import { useOssStore } from '@/stores/modules/oss'
import { useDataStore } from '@/stores/modules/data'
import useOss from '@/hooks/useOss'

export default function() {
  const pwdListKey = 'password'
  const ossVersionKey = 'ossVersion'
  const ossStore = useOssStore()
  const dataStore = useDataStore()
  const { getFile } = useOss()

  function initData() {
    getFile(pwdListKey).then((json) => {
      // console.log('syncToLocal json', json)
      // 如果没值, 忽略
      if (!json) {
        return
      }
      // 先进行解析
      const ossSyncObj: OssSyncObj = JSON.parse(json)

      // 判断是否有值
      if (!(ossSyncObj.groupList && ossSyncObj.groupList.length > 0
        && ossSyncObj.pwdInfoList && ossSyncObj.pwdInfoList.length > 0)) {
        return
      }
      // 解析数据 放到 data中
      ossSyncObj.groupList.forEach((group) => {
        group.pwdList = []
        ossSyncObj.pwdInfoList.forEach((pwdInfo) => {
          if (pwdInfo.group_id === group.id) {
            group.pwdList.push(pwdInfo)
          }
        })
      })
      dataStore.setGroupList(ossSyncObj.groupList)
    })
  }

  return { initData }
}